package com.todo.sight.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.CityTypeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.sight.application.convert.CityInfoConvert;
import com.todo.sight.application.entity.bo.CityInfoBO;
import com.todo.sight.application.frontend.service.CityInfoAggService;
import com.todo.sight.application.iface.frontend.request.QueryCityDetailParam;
import com.todo.sight.application.iface.frontend.request.QueryCityInfoByNameParam;
import com.todo.sight.application.iface.frontend.request.QueryCityInfoListParam;
import com.todo.sight.application.iface.frontend.response.CityDetailVO;
import com.todo.sight.application.iface.frontend.response.CityInfoVO;
import com.todo.sight.application.service.CityInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 前台城市信息聚合Service
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:44
 */
@Slf4j
@Service("cityInfoAggService")
public class CityInfoAggServiceImpl implements CityInfoAggService {

    @Resource
    private CityInfoService cityInfoService;

    @Override
    public BizResponse<List<CityInfoVO>> queryCityInfoList(QueryCityInfoListParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        CityTypeEnum cityType = CityTypeEnum.getByType(param.getCityType());
        if (!CityTypeEnum.PROVINCE.equals(cityType) && param.getParentId() == null) {
            log.error("CityInfoAggService#queryCityInfoList 查询异常，非省份数据查询，父Id为空，无法查询 入参：【{}】", JSONObject.toJSONString(param));
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "查询异常，请重试查询");
        }
        if (CityTypeEnum.PROVINCE.equals(cityType)) {
            //当查询为省信息时候，默认父Id为1
            param.setParentId(1L);
        }
        List<CityInfoBO> cityInfoBOList = cityInfoService.queryCityInfoByParentId(param.getParentId(), CityTypeEnum.getByType(param.getCityType()));
        if (CollectionUtils.isEmpty(cityInfoBOList)) {
            log.info("CityInfoAggService#queryCityInfoList 查询城市信息为空 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.success();
        }
        List<CityInfoVO> cityInfoVOList = CityInfoConvert.convertToCityInfoVOList(cityInfoBOList);
        return BizResponse.success(cityInfoVOList);
    }

    @Override
    public BizResponse<CityDetailVO> queryCityDetail(QueryCityDetailParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        CityInfoBO cityInfoBO = cityInfoService.queryCityInfoById(Long.valueOf(param.getCityId()));
        CityDetailVO cityDetailVO = CityInfoConvert.convertToCityDetailVO(cityInfoBO);
        return BizResponse.success(cityDetailVO);
    }

    @Override
    public BizResponse<List<CityInfoVO>> queryCityInfoByName(QueryCityInfoByNameParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<CityInfoBO> cityInfoBOList = cityInfoService.searchCityInfoByName(param.getCityName());
        if (CollectionUtils.isEmpty(cityInfoBOList)) {
            log.info("CityInfoAggService#queryCityInfoByName 查询城市信息为空 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.success();
        }
        List<CityInfoVO> cityInfoVOList = CityInfoConvert.convertToCityInfoVOList(cityInfoBOList);
        return BizResponse.success(cityInfoVOList);
    }
}
